package com.forgweb.magnet.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.forgweb.magnet.bean.BTSpreadBean;


public class BTSpreadSearch {
	
	Logger logger = Logger.getLogger(BTSpreadSearch.class);
	
	private static String base = "http://www.btspread.com/search/";
	
	private String url ;
	
	private String key;
	
	public static String getBase() {
		return base;
	}

	public static void setBase(String base) {
		BTSpreadSearch.base = base;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public BTSpreadSearch(){}
	
	public BTSpreadSearch(String k){
		this.key = k;
	}
	
	
	public static void main(String[] args) {

//		String key = "蜘蛛侠2"; // %E8%9C%98%E8%9B%9B%E4%BE%A02
//		String url ="http://www.btspread.com/search/" + key;
//		
//		BTSpreadSearch bt = new BTSpreadSearch(url);
//		Document doc =bt.getDocfomeURL(url);
//		
//		Elements magnetsTRs= doc.select("table.magnet-list tbody").first().select("tr");
//		
//		List <BTSpreadBean> BTlist = new ArrayList<BTSpreadBean>();
//		
//		for(Element e : magnetsTRs){
//			Element title = e.select("a[title]").first();
//			Element size = e.select(".files-size").first();
//			Element date = e.select(".convert-date").first();
//			
//			if(title != null && size != null &&  date!= null){
//				String t = title.attr("title");
//				String u = title.attr("href");
//				String d = date.html();
//				String s = size.html();
//				String m = "magnet:?xt=urn:btih:" + u.substring(u.lastIndexOf("/")+1);
//				BTSpreadBean b = new BTSpreadBean(t,m,d,s,u);
//				System.out.println(b.toString());
//				BTlist.add(b);
//			}
//		}
		
		BTSpreadSearch bt = new BTSpreadSearch();
		List <BTSpreadBean> BTlist =bt.getBTResult("蜘蛛侠2");
		
		System.out.println(BTlist.size());		
		
		//System.out.println(magnetBody.toString());	
	}
	
	private Document getDocfomeURL(String url){
		
		Document doc = null;
		try {
			doc = Jsoup.connect(url).get();
		} catch (IOException e) {
			logger.error("kan>doc_exception:" + e.toString());

			return null;
		}
		
		return doc;
	}
	
	
	public List<BTSpreadBean> getBTResult(){
		List <BTSpreadBean> BTlist = new ArrayList<BTSpreadBean>();
		
		if(this.key == null  ||  "".equals(this.key)){
			return BTlist;
		}
		this.url = base + key;
		Document doc =getDocfomeURL(url);
		
		if(doc == null){
			return BTlist;
		}
		
		Elements tbodys = doc.select("table.magnet-list tbody");
		if(tbodys == null || tbodys.size() ==0 ){
			return BTlist;
		}
		Elements magnetsTRs= tbodys.first().select("tr");
		for(Element e : magnetsTRs){
			Element title = e.select("a[title]").first();
			Element size = e.select(".files-size").first();
			Element date = e.select(".convert-date").first();
			
			if(title != null && size != null &&  date!= null){
				String t = title.attr("title");
				String u = title.attr("href");
				String d = date.html();
				String s = size.html();
				String m = "magnet:?xt=urn:btih:" + u.substring(u.lastIndexOf("/")+1);
				BTlist.add(new BTSpreadBean(t,m,d,s,u));
			}
		}
		
		return BTlist;
	}
	
	public List<BTSpreadBean> getBTResult(String key){
		this.key = key;
		return getBTResult();
	}
}
